package cn.edu.nchu.comic_strip.ui.bookshelf;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.util.List;
import cn.edu.nchu.comic_strip.App;
import cn.edu.nchu.comic_strip.R;
import cn.edu.nchu.comic_strip.database.dao.BookDao;
import cn.edu.nchu.comic_strip.databinding.FragmentBookshelfBinding;
import cn.edu.nchu.comic_strip.entity.Book;

public class BookshelfFragment extends Fragment {

    private FragmentBookshelfBinding binding;
    private Context mContext;
    private List<Book> bookList;
    private ListView listView;
    private BookListAdapter adapter;
    private BookDao bookDao = App.getAppDatabase().getBookDao();

    private TextView timeTextView;

    private static final String PREFS_NAME = "MyPrefsFile";
    private static final String FRAGMENT_NAME = "ComicFragment";

    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentBookshelfBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // Initialize mContext
        mContext = getContext();

        // Get the book list
        bookList = bookDao.getAllBooks();

        // Set up ListView and adapter
        listView = root.findViewById(R.id.book_list);
        adapter = new BookListAdapter(mContext, bookList);
        listView.setAdapter(adapter);

        // Set click event
        listView.setOnItemClickListener((adapterView, view, position, id) -> {
            Intent intent = new Intent("cn.edu.nchu.comic_strip.ui.bookpage.BookPageActivity");
            intent.putExtra("bookId", bookList.get(position).getId());  // Pass bookId
            startActivity(intent);  // Start Activity
        });

        // Initialize timeTextView
        timeTextView = root.findViewById(R.id.time_value);

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Load saved time and display
        SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
        int totalTime = settings.getInt(FRAGMENT_NAME, 0);
        if (totalTime == 0) {
            timeTextView.setVisibility(View.GONE);
        } else {
            timeTextView.setVisibility(View.VISIBLE);
            timeTextView.setText(formatTime(totalTime));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public String formatTime(long totalTime) {
        long hours = totalTime / 3600;
        long minutes = (totalTime % 3600) / 60;
        long seconds = totalTime % 60;

        StringBuilder stringBuilder = new StringBuilder();

        if (hours > 0) {
            stringBuilder.append(hours).append("小时");
        }

        if (minutes > 0) {
            stringBuilder.append(minutes).append("分");
        }

        if (seconds > 0 || (hours == 0 && minutes == 0)) {
            stringBuilder.append(seconds).append("秒");
        }

        return stringBuilder.toString();
    }
}
